package com.mcxx.modules.minimumliving.service.impl;

import com.mcxx.modules.minimumliving.service.QueryService;
import com.mcxx.modules.miniSalvation.family.dao.read.FamilySupportReadDAO;
import com.mcxx.modules.miniSalvation.family.dto.FamilyMemberDTO;
import com.mcxx.modules.miniSalvation.family.dto.MemberAssetsDTO;
import com.mcxx.modules.miniSalvation.family.dto.MininumLivingApplyDTO;
import com.mcxx.modules.miniSalvation.family.entity.*;
import com.mcxx.modules.miniSalvation.family.service.read.*;
import com.mcxx.modules.miniSalvation.order.entity.OrderEntity;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.util.Constant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class QueryServiceImpl implements QueryService {

    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private FamilyMemberReadService familyMemberReadService;
    @Autowired
    private FamilyMemberHistoryReadService familyMemberHistoryReadService;
    @Autowired
    private FamilySupportReadService familySupportReadService;
    @Autowired
    private FamilySupportHistoryReadService familySupportHistoryReadService;
    @Autowired
    private SpecialPeopleReadService specialPeopleReadService;
    @Autowired
    private SpecialPeopleHistoryReadService specialPeopleHistoryReadService;
    @Autowired
    private MemberAssetsReadService memberAssetsReadService;
    @Autowired
    private FamilyMainReadService familyMainReadService;
    @Autowired
    private FamilyMainHistoryReadService familyMainHistoryReadService;


    @Override
    public MininumLivingApplyDTO getOrderDetail(String orderId) {

        String state = orderReadService.getStateById((orderId));


        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            return orderReadService.getDetailById(orderId);
        }


        return orderReadService.getFinishedDetailById(orderId);
    }


    @Override
    public List listFamilyMember(String orderId) {

        OrderEntity orderEntity = orderReadService.getById(orderId);

        if (Constant.OrderType.NEW.equals(orderEntity.getType())) {

            if (!Constant.HIS_ABLE_STATUS.contains(orderEntity.getState())) {
                return familyMemberReadService.listNormalMemberByFamilyId(orderEntity.getFamilyId());
            }


            return familyMemberHistoryReadService.listByOrderId(orderId);
        }


        if (!Constant.HIS_ABLE_STATUS.contains(orderEntity.getState())) {
            return familyMemberReadService.listTempByOrderId(orderId);
        }


        return familyMemberHistoryReadService.listTempByOrderId(orderId);
    }


    @Override
    public List listFamilySuppor(String orderId) {

        OrderEntity orderEntity = orderReadService.getById(orderId);

        if (!Constant.HIS_ABLE_STATUS.contains(orderEntity.getState())) {
            return familySupportReadService.getFamilySupportList(orderEntity.getFamilyId());
        }


        return familySupportHistoryReadService.getFamilySupportHistoryList(orderId);
    }

    @Override
    public List listFamilySpecial(String orderId) {

        OrderEntity orderEntity = orderReadService.getById(orderId);

        if (!Constant.HIS_ABLE_STATUS.contains(orderEntity.getState())) {
            return specialPeopleReadService.getFamilySpecialList(orderEntity.getFamilyId());
        }


        return specialPeopleHistoryReadService.getFamilySpecialHistoryList(orderId);
    }


    @Override
    public FamilyMemberEntity getFamilyMember(String id, String orderId) {

        String state = orderReadService.getStateById(orderId);


        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            return familyMemberReadService.getById(id);
        }


        FamilyMemberHistoryEntity memberHistoryEntity = familyMemberHistoryReadService.getById(id);
        FamilyMemberEntity memberEntity = new FamilyMemberEntity();
        BeanUtils.copyProperties(memberHistoryEntity, memberEntity);

        return memberEntity;
    }


    @Override
    public FamilyMemberDTO getFamilyMemberBankInfo(String id, String orderId, String businessCode) {

        String state = orderReadService.getStateById(orderId);


        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            return familyMemberReadService.getByIdAndBusinessCode(id,businessCode);
        }


        return familyMemberHistoryReadService.getByIdAndBusinessCode(id,businessCode);
    }

    @Override
    public FamilySpecialEntity getFamilySpecialById(String id,String orderId) {

        String state = orderReadService.getStateById((orderId));


        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            return specialPeopleReadService.getSpecialPeopleById(id);
        }


        FamilySpecialHistoryEntity familySpecialHistoryEntity = specialPeopleHistoryReadService.queryFamilySpecialHistory(id);
        FamilySpecialEntity familySpecialEntity = new FamilySpecialEntity();
        BeanUtils.copyProperties(familySpecialHistoryEntity, familySpecialEntity);

        return familySpecialEntity;
    }


    @Override
    public FamilySupportEntity getFamilySupportByIds(String id, String orderId) {

        String state = orderReadService.getStateById((orderId));


        if (!Constant.HIS_ABLE_STATUS.contains(state)) {
            return familySupportReadService.getFamilySupportById(id);
        }


        FamilySupportHistoryEntity familySupportHistoryEntity = familySupportHistoryReadService.queryFamilySupportHistory(id);
        FamilySupportEntity familySupportEntity = new FamilySupportEntity();
        BeanUtils.copyProperties(familySupportHistoryEntity, familySupportEntity);

        return familySupportEntity;
    }


    @Override
    public Map<String, Object> getMemberAssets(String familyId, String orderId){
        Map<String, Object> rstMap = new HashMap<>();


        List<MemberAssetsDTO> memberPropertyDTOList =  memberAssetsReadService.queryListByFamilyId(familyId, orderId,  Constant.MemberAssetsType.CAI_CHAN);
        rstMap.put("memberPropertyDTOList", memberPropertyDTOList);

        List<MemberAssetsDTO> memberIncomeDTOList = memberAssetsReadService.getMemberAssetsList(familyId, orderId, Constant.MemberAssetsType.SHOU_RU);
        rstMap.put("memberIncomeDTOList", memberIncomeDTOList);

        List<MemberAssetsDTO> memberDeductionDTOList = memberAssetsReadService.getMemberAssetsList(familyId, orderId,Constant.MemberAssetsType.DI_KOU_XIANG);
        rstMap.put("memberDeductionDTOList", memberDeductionDTOList);


        String familyRevenue = "0";

        String state = orderReadService.getStateById((orderId));
        if (!Constant.HIS_ABLE_STATUS.contains(state)) {

            familyRevenue = familyMainReadService.selectFamilyRevenue(familyId);
        }else{

            familyRevenue = familyMainHistoryReadService.getFamilyRevenue(orderId);
        }
        String incomeRemark = memberAssetsReadService.getAssetsRemark(familyId, orderId, Constant.MemberAssetsType.SHOU_RU_BEIZHU);
        String assetsRemark = memberAssetsReadService.getAssetsRemark(familyId, orderId, Constant.MemberAssetsType.ZHICHU_BEIZHU);
        rstMap.put("incomeRemark",incomeRemark);
        rstMap.put("assetsRemark",assetsRemark);
        rstMap.put("familyRevenue", familyRevenue);
        return rstMap;
    }
}
